// pages/details/details.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_chezhu:{},
    /*  */
    showQrCode: false,
    qrCodeSrc: '',
    cardNo: '',   //卡券no
    cardDetail:{
      price:'',
      cardName:'',
      useStartTime:'',
      useEndTime:'',
      useStoreId: '',
      useStoreName: '',
      useLicenseNo:''
    },
    /*  */
    useData:[
      {
        name:'立即使用',
        openAnimationLoad:false
      }
    ],
    /*  */
    noUseData:[
      {
        name:'暂不使用',
        openAnimationLoad:false
      }
    ],
    /*  */
    status:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_chezhu: app.globalData.get_userInfo()
    })
    //
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('open-card-detail', data => {
      console.log(data)
      this.setData({
        showQrCode: false,
        cardNo: data.cardno,
        status: data.status
      })
      /*  */
      this.getOneCard()
      /*  */
    })
  },
  /* 刷新二维码 */
  refreshQrCode: function(){
    wx.showToast({
      icon:'none',
      title:'刷新二维码图片'
    })
    this.setData({
      showQrCode: true,
      qrCodeSrc:`${app.globalData.Url}/wx/getCradQrCode?cardNo=${this.data.cardNo}`
    })
  },
  /* 生成卡片二维码 */
  getCradQrCode: function (e) {
    let { index } = e.currentTarget.dataset
    return app.globalData.setNeedAnimation.call(this,'useData',this.data.useData,index).then(()=>{
      this.setData({
        showQrCode: true,
        qrCodeSrc:`${app.globalData.Url}/wx/getCradQrCode?cardNo=${this.data.cardNo}`
      })
    })
  },
  /* 返回卡片列表 */
  backCardList: function (e) {
    let { index } = e.currentTarget.dataset
    return app.globalData.setNeedAnimation.call(this,'noUseData',this.data.noUseData,index).then(()=>{
      this.setData({
        showQrCode: false
      })
      wx.navigateBack({
        complete: (res) => { }
      })
    })
  },
  /* 获取卡券详情 */
  getOneCard: function () {
    wx.showLoading({
      title: '获取卡券详情',
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/wx/getOneCard',
        method: 'POST',
        header:this.data.userInfo_chezhu.header,
        data: util.json2Form({
          cardNo:this.data.cardNo
        }),
        success: res => {
          console.log(res)
          //判断token是否过期
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1') {
            let tmpData = app.globalData.extend({},res.data.body)
            tmpData.timeLong = app.globalData.dateFormat2(tmpData.useStartTime)+'-'+app.globalData.dateFormat2(tmpData.useEndTime)
            tmpData.useTime = app.globalData.dateFormatYMDHM(tmpData.useTime)
            this.setData({
              cardDetail:tmpData
            })
            resolve()
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.msg
            })
            reject()
          }
        },
        fail: err => {
          console.log(err)
          wx.showToast({
            icon: 'none',
            title: '获取卡券详情失败'
          })
          reject()
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  /* 商户页面 */
  showSupportStore: function(e){
    wx.navigateTo({
      url:'/pages/stores/stores',
      success: res => {
        res.eventChannel.emit('open-stores-page',{
          cardNo:this.data.cardNo,
          cardName: this.data.cardDetail.cardName
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})